package com.codeoss.framework.view.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.report.util.DetailReportModel;
import com.codeoss.framework.report.util.DetailReportUtil;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.report.DetailReport;
import com.codeoss.module.ir.model.IrReport;
import com.codeoss.module.ir.service.impl.IrReportService;


@Service("detailreportComponentService")
public class DetailReportComponentService extends ComponentService{
	@Autowired
	private IrReportService irReportService;
	@Autowired
	private DetailReportUtil detailReportUtil;
	@Override
	public void processEvent(YunContext yunContext, Component component, String eventType) {
		super.processEvent(yunContext, component, eventType);
		if ("pageEvent".equals(eventType)) {
			processPageEvent(yunContext,component);
		}
	}
	
	protected void processPageEvent(YunContext yunContext, Component component) {
		DetailReport detailReport=(DetailReport)component;
		int page=Integer.parseInt(yunContext.getViewRequest().getData().get(component.getId()+"_page"));
		QueryWrapper<IrReport> query=new QueryWrapper<IrReport>();
		query.eq("code", detailReport.getReportCode());
		IrReport irReport=irReportService.getOne(query);
		String jsonContent=irReport.getJsonContent();
		com.codeoss.framework.report.def.DetailReport report=null;
		if(!StringUtils.isEmpty(jsonContent)) {
			report=detailReportUtil.buildDetailReportFromIrReport(irReport);
		}
		DetailReportModel detailReportModel=detailReportUtil.buildDetailReportData(report,page);
		yunContext.setAttribute(component.getId()+"_reportdata", detailReportModel);
		List<Component> renderComponentList=new ArrayList<Component>();
		renderComponentList.add(component);
		yunContext.setRenderComponentList(renderComponentList);
	}
}
